import { createRouter, createWebHashHistory } from 'vue-router'
import layout from '../layout/layout.vue'

import Editor from '@/view/editor/index.vue'

const redirect = localStorage.getItem('dev_router_path') || '/editor'

const routes = [
  {
    path: '',
    component: layout,
    redirect,
    children: [
      {
        name: 'editor',
        path: '/editor',
        component: Editor,
        meta: {
          title: '码粒3D编辑器',
          keywords: '模型,渲染,lowcode,editor,3d,三维,低代码,编辑器,无代码,threejs,',
          description: '一款基于three.js快速使用的三维低代码编辑器'
        }
      }
    ]
  }
]

routes.push({ path: '/:pathMatch(.*)*', redirect: redirect })

const router = createRouter({ history: createWebHashHistory(), routes })

router.beforeEach((to, from, next) => {

  if (to.meta) setMetaContent(to.meta)

  next()

})

export function setMetaContent(meta) {

  if (meta.title) document.title = meta.title

  if (meta.keywords) document.querySelector('meta[name="keywords"]').setAttribute('content', meta.keywords)

  if (meta.description) document.querySelector('meta[name="description"]').setAttribute('content', meta.description)

}

export default router
